<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>checkbox.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_checkbox.js.html">checkbox.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2009 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Tristate checkbox widget.
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/checkbox.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.Checkbox&#39;);
<a name="line22"></a>goog.provide(&#39;goog.ui.Checkbox.State&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line26"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * 3-state checkbox widget. Fires CHECK or UNCHECK events before toggled and
<a name="line33"></a> * CHANGE event after toggled by user.
<a name="line34"></a> * TODO: Add keyboard support.
<a name="line35"></a> *
<a name="line36"></a> * @param {goog.ui.Checkbox.State} opt_checked Checked state to set.
<a name="line37"></a> * @constructor
<a name="line38"></a> * @extends {goog.ui.Component}
<a name="line39"></a> */
<a name="line40"></a>goog.ui.Checkbox = function(opt_checked) {
<a name="line41"></a>  goog.ui.Component.call(this);
<a name="line42"></a>  if (goog.isDef(opt_checked)) {
<a name="line43"></a>    this.checked_ = opt_checked;
<a name="line44"></a>  }
<a name="line45"></a>};
<a name="line46"></a>goog.inherits(goog.ui.Checkbox, goog.ui.Component);
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * Possible checkbox states.
<a name="line51"></a> * @enum {?boolean}
<a name="line52"></a> */
<a name="line53"></a>goog.ui.Checkbox.State = {
<a name="line54"></a>  CHECKED: true,
<a name="line55"></a>  UNCHECKED: false,
<a name="line56"></a>  UNDETERMINED: null
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * CSS class for checkbox.
<a name="line62"></a> * @type {string}
<a name="line63"></a> */
<a name="line64"></a>goog.ui.Checkbox.CSS_CLASS = goog.getCssName(&#39;goog-checkbox&#39;);
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Checked state of the checkbox.
<a name="line69"></a> * @type {goog.ui.Checkbox.State}
<a name="line70"></a> * @private
<a name="line71"></a> */
<a name="line72"></a>goog.ui.Checkbox.prototype.checked_ = goog.ui.Checkbox.State.UNCHECKED;
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Whether the checkbox is enabled.
<a name="line77"></a> * @type {boolean}
<a name="line78"></a> * @private
<a name="line79"></a> */
<a name="line80"></a>goog.ui.Checkbox.prototype.enabled_ = true;
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Label element bound to the checkbox.
<a name="line85"></a> * @type {Element}
<a name="line86"></a> * @private
<a name="line87"></a> */
<a name="line88"></a>goog.ui.Checkbox.prototype.label_ = null;
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * @return {goog.ui.Checkbox.State} Checked state of the checkbox.
<a name="line93"></a> */
<a name="line94"></a>goog.ui.Checkbox.prototype.getChecked = function() {
<a name="line95"></a>  return this.checked_;
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * @return {boolean} Whether the checkbox is checked.
<a name="line101"></a> */
<a name="line102"></a>goog.ui.Checkbox.prototype.isChecked = function() {
<a name="line103"></a>  return this.checked_ == goog.ui.Checkbox.State.CHECKED;
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * @return {boolean} Whether the checkbox is not checked.
<a name="line109"></a> */
<a name="line110"></a>goog.ui.Checkbox.prototype.isUnchecked = function() {
<a name="line111"></a>  return this.checked_ == goog.ui.Checkbox.State.UNCHECKED;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * @return {boolean} Whether the checkbox is in partially checked state.
<a name="line117"></a> */
<a name="line118"></a>goog.ui.Checkbox.prototype.isUndetermined = function() {
<a name="line119"></a>  return this.checked_ == goog.ui.Checkbox.State.UNDETERMINED;
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Sets the checked state of the checkbox.
<a name="line125"></a> * @param {goog.ui.Checkbox.State} checked The checked state to set.
<a name="line126"></a> */
<a name="line127"></a>goog.ui.Checkbox.prototype.setChecked = function(checked) {
<a name="line128"></a>  if (checked != this.checked_) {
<a name="line129"></a>    this.checked_ = checked;
<a name="line130"></a>    this.updateView();
<a name="line131"></a>  }
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * @return {boolean} Whether the checkbox is enabled.
<a name="line137"></a> */
<a name="line138"></a>goog.ui.Checkbox.prototype.isEnabled = function() {
<a name="line139"></a>  return this.enabled_;
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * Sets whether the checkbox is enabled.
<a name="line145"></a> * @param {boolean} enabled New value of the enabled flag.
<a name="line146"></a> */
<a name="line147"></a>goog.ui.Checkbox.prototype.setEnabled = function(enabled) {
<a name="line148"></a>  if (enabled != this.enabled_) {
<a name="line149"></a>    this.enabled_ = enabled;
<a name="line150"></a>    this.updateView();
<a name="line151"></a>  }
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * Binds an HTML element to the checkbox which if clicked toggles the checkbox.
<a name="line157"></a> * Behaves the same way as the &#39;label&#39; HTML tag. The label element has to be the
<a name="line158"></a> * direct or non-direct ancestor of the checkbox element because it will get the
<a name="line159"></a> * focus when keyboard support is implemented.
<a name="line160"></a> *
<a name="line161"></a> * @param {Element} label The label control to set. If null, only the checkbox
<a name="line162"></a> *     reacts to clicks.
<a name="line163"></a> */
<a name="line164"></a>goog.ui.Checkbox.prototype.setLabel = function(label) {
<a name="line165"></a>  if (this.isInDocument()) {
<a name="line166"></a>    this.exitDocument();
<a name="line167"></a>    this.label_ = label;
<a name="line168"></a>    this.enterDocument();
<a name="line169"></a>  } else {
<a name="line170"></a>    this.label_ = label;
<a name="line171"></a>  }
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Toggles the checkbox. State transitions:
<a name="line177"></a> * &lt;ul&gt;
<a name="line178"></a> *   &lt;li&gt;unchecked -&gt; checked
<a name="line179"></a> *   &lt;li&gt;undetermined -&gt; checked
<a name="line180"></a> *   &lt;li&gt;checked -&gt; unchecked
<a name="line181"></a> * &lt;/ul&gt;
<a name="line182"></a> */
<a name="line183"></a>goog.ui.Checkbox.prototype.toggle = function() {
<a name="line184"></a>  this.checked_ = this.checked_ ? goog.ui.Checkbox.State.UNCHECKED :
<a name="line185"></a>      goog.ui.Checkbox.State.CHECKED;
<a name="line186"></a>  this.updateView();
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/** @inheritDoc */
<a name="line191"></a>goog.ui.Checkbox.prototype.createDom = function() {
<a name="line192"></a>  this.decorateInternal(goog.dom.$dom(&#39;span&#39;));
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/** @inheritDoc */
<a name="line197"></a>goog.ui.Checkbox.prototype.decorateInternal = function(element) {
<a name="line198"></a>  goog.ui.Checkbox.superClass_.decorateInternal.call(this, element);
<a name="line199"></a>  goog.dom.classes.add(element, goog.ui.Checkbox.CSS_CLASS);
<a name="line200"></a>  this.updateView();
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/** @inheritDoc */
<a name="line205"></a>goog.ui.Checkbox.prototype.enterDocument = function() {
<a name="line206"></a>  goog.ui.Checkbox.superClass_.enterDocument.call(this);
<a name="line207"></a>  this.getHandler().listen(this.label_ || this.getElement(),
<a name="line208"></a>      goog.events.EventType.CLICK, this.handleClick_);
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Updates the CSS class names after the checked state has changed.
<a name="line214"></a> * @protected
<a name="line215"></a> */
<a name="line216"></a>goog.ui.Checkbox.prototype.updateView = function() {
<a name="line217"></a>  var el = this.getElement();
<a name="line218"></a>  if (el) {
<a name="line219"></a>    goog.dom.classes.enable(el,
<a name="line220"></a>        goog.getCssName(goog.ui.Checkbox.CSS_CLASS, &#39;unchecked&#39;),
<a name="line221"></a>        this.isUnchecked());
<a name="line222"></a>    goog.dom.classes.enable(el,
<a name="line223"></a>        goog.getCssName(goog.ui.Checkbox.CSS_CLASS, &#39;checked&#39;),
<a name="line224"></a>        this.isChecked());
<a name="line225"></a>    goog.dom.classes.enable(el,
<a name="line226"></a>        goog.getCssName(goog.ui.Checkbox.CSS_CLASS, &#39;undetermined&#39;),
<a name="line227"></a>        this.isUndetermined());
<a name="line228"></a>    goog.dom.classes.enable(el,
<a name="line229"></a>        goog.getCssName(goog.ui.Checkbox.CSS_CLASS, &#39;disabled&#39;),
<a name="line230"></a>        !this.enabled_);
<a name="line231"></a>  }
<a name="line232"></a>};
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * Handles the click event.
<a name="line237"></a> * @private
<a name="line238"></a> */
<a name="line239"></a>goog.ui.Checkbox.prototype.handleClick_ = function() {
<a name="line240"></a>  var eventType = this.checked_ ? goog.ui.Component.EventType.UNCHECK :
<a name="line241"></a>      goog.ui.Component.EventType.CHECK;
<a name="line242"></a>  if (this.enabled_ &amp;&amp; this.dispatchEvent(eventType)) {
<a name="line243"></a>    this.toggle();
<a name="line244"></a>    this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line245"></a>  }
<a name="line246"></a>};
</pre>


</body>
</html>
